import { OrderService } from './modules/order/order.service';
import { CarInfoService } from './modules/car_info/car_info.service';
import { CarBrandService } from './modules/car_brand/car_brand.service';
import { UserService } from './modules/user/user.service';
import { ApiResult } from 'src/config';
import { Controller, Get } from '@nestjs/common';
import { ApiOperation } from '@nestjs/swagger';

@Controller()
export class AppController {
  constructor(
    private readonly userService: UserService,
    private readonly carBrandService: CarBrandService,
    private readonly carInfoService: CarInfoService,
    private readonly orderService: OrderService,
  ) {}

  @ApiOperation({ summary: '获取数据统计' })
  @Get('data')
  async getData(): Promise<ApiResult> {
    const result = await Promise.all([
      this.userService.getTotal(),
      this.carBrandService.getTotal(),
      this.carInfoService.getTotal(),
      this.orderService.getTotal(),
    ]);
    const timeResult = await Promise.all([
      this.carInfoService.getListTime('2010'),
      this.carInfoService.getListTime('2011'),
      this.carInfoService.getListTime('2012'),
      this.carInfoService.getListTime('2013'),
      this.carInfoService.getListTime('2014'),
      this.carInfoService.getListTime('2015'),
      this.carInfoService.getListTime('2016'),
      this.carInfoService.getListTime('2017'),
      this.carInfoService.getListTime('2018'),
    ]);
    const car = timeResult.map((item, index) => {
      return {
        year: 2010 + index,
        value: item,
      };
    });
    return ApiResult.SUCCESS(
      {
        user: result[0],
        brand: result[1],
        car: {
          total: result[2],
          car,
        },
        order: result[3],
      },
      0,
      '获取成功！',
    );
  }
}
